package com.zwy.ubts.controller;

import com.yg.yws.annotation.YWebPath;
import com.yg.yws.annotation.YWebServlet;
import com.yg.yws.beans.RetType;
import com.zwy.ubts.bean.Admin;
import com.zwy.ubts.bean.Book;
import com.zwy.ubts.bean.BookType;
import com.zwy.ubts.bean.User;
import com.zwy.ubts.service.*;
import com.zwy.ubts.service.impl.*;
import com.zwy.ubts.utils.DataInfo;
import com.zwy.ubts.utils.RetJsonUtils;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Zhou Wenying
 * @Description:
 * @Date Created in 17:31 2023/4/12
 */
@YWebServlet
public class AdminController {
    private AdminService adminService = new AdminServiceImpl();
    private BookService bookService = new BookServiceImpl();
    private LbService lbService = new LbServiceImpl();
    private UserService userService = new UserServiceImpl();
    private BookTypeService bookTypeService = new BookTypeServiceImpl();

    @YWebPath("/admin/AdminIndex")
    public RetType AdminIndex(){
        return RetType.retForward(DataInfo.PAGE_ROOT+"admin_index.jsp");
    }

    @YWebPath("/admin/AdminLogin")
    public RetType AdminLogin(){
        return RetType.retForward(DataInfo.PAGE_ROOT+"admin.jsp");
    }

    @YWebPath("/admin/doAdminLogin")
    public RetType AdminLogin(String username, String password, HttpSession session){
        Admin admin=adminService.selectAdmin(new Admin(username,password));
        Map map = new HashMap();
        if (admin != null){
            session.setAttribute("admin",admin);
            //登录成功
            map.put("admin",admin);
            return RetType.retForward(DataInfo.PAGE_ROOT+"admin_index.jsp",map);
        }else {
            //登录失败
            map.put(DataInfo.MESSAGE,DataInfo.LOGIN_ERR_FILD);
            return RetType.retForward(DataInfo.PAGE_ROOT+"admin.jsp",map);
        }
    }

    @YWebPath("/admin/adminUser")
    public RetType adminUser(Integer userRole){
        List<User> users = userService.selectUser();
        List<User> users1 = userService.selectUserByRole(userRole);
        Map map = new HashMap();
        map.put("user",users);
        map.put("user1",users1);

        return RetType.retForward(DataInfo.PAGE_ROOT+"admin_user.jsp",map);
    }
    @YWebPath("/admin/adminBook")
    public RetType adminBook(){
        List<Book> book1 = bookService.selectAllBook(""); //全部书籍
        List<Book> book2 = bookService.selectBookByCheck(); //未审核书籍
        List<Integer> integers = lbService.selectLb();//加入条幅的书籍
        List<Book> lbBook = new ArrayList<>();
        for (int i = 0; i < integers.size(); i++) {
            Book book = bookService.selectByBookId(integers.get(i));
            if(book == null){
                lbService.deleteLb(integers.get(i));
                continue;
            }
            lbBook.add(book);
        }
        Map map = new HashMap();
        map.put("book1",book1);
        map.put("book2",book2);
        map.put("lbBook",lbBook);
        return RetType.retForward(DataInfo.PAGE_ROOT+"admin_book.jsp",map);
    }

    @YWebPath("/admin/doAdminAddTf")
    public RetType doAdminAddTf(Integer bookId){
        Book book = bookService.selectByBookId(bookId);
        List<Integer> integers = lbService.selectLb();
        boolean isTrue = false;

        List<Book> lbBook = new ArrayList<>();
        for (int i = 0; i < integers.size(); i++) {
            if(integers.get(i).equals(bookId)){
                isTrue=true;
                continue;
            }
        }
        if(isTrue){
            return RetType.retJson(RetJsonUtils.toJson("执行审核：书籍《"+book.getBookName()+"》该条幅已存在"));

        }else
        {
            Integer integer = lbService.insertLb(bookId);
            return RetType.retJson(RetJsonUtils.toJson("执行审核：书籍《"+book.getBookName()+"》新增条幅成功"));
        }
    }

    @YWebPath("/admin/doAdminBook")
    public RetType doAdminBook(Integer bookId,Integer check){
        Book book = bookService.selectByBookId(bookId);
        book.setBookCheck(check);
        bookService.updateBookById(book);
        if (check == 1){
            return RetType.retJson(RetJsonUtils.toJson("执行审核：书籍《"+book.getBookName()+"》审核通过"));
        }else {
            return RetType.retJson(RetJsonUtils.toJson("执行审核：书籍《"+book.getBookName()+"》审核未通过"));
        }
    }

    @YWebPath("/admin/doAdminDelTf")
    public RetType doAdminDelTf(Integer bookId){
        Book book = bookService.selectByBookId(bookId);
        Integer integer = lbService.deleteLb(bookId);

        return RetType.retJson(RetJsonUtils.toJson("执行审核：书籍《"+book.getBookName()+"》移除条幅成功"));
    }

    @YWebPath("/admin/adminOrder")
    public RetType adminOrder(){
        return RetType.retForward(DataInfo.PAGE_ROOT+"admin_order.jsp");
    }
    @YWebPath("/admin/adminType")
    public RetType adminType(){
        List<BookType> bookTypes = bookTypeService.queryBookTypes();
        Map map = new HashMap();
        map.put("bookTypes",bookTypes);
        return RetType.retForward(DataInfo.PAGE_ROOT+"admin_type.jsp",map);
    }

    @YWebPath("/admin/doAddBookType")
    public RetType doAddBookType(String bookTypeName){
        Map map = new HashMap();
        Integer integer = bookTypeService.insertBookType(new BookType(0, bookTypeName));
        map.put("msg","添加成功！");
        List<BookType> bookTypes = bookTypeService.queryBookTypes();

        map.put("bookTypes",bookTypes);
        return RetType.retForward(DataInfo.PAGE_ROOT+"admin_type.jsp",map);
    }

    //"/admin/doDeleteBookType?bookTypeId="+id,
    @YWebPath("/admin/doDeleteBookType")
    public RetType doDeleteBookType(Integer bookTypeId){
        List<Book> books = bookService.queryBookByClassify(bookTypeId, "");
        if (books != null && books.size()>0){
            return RetType.retJson(RetJsonUtils.toJsonFail("有书籍还使用这个类型！"));
        }
        BookType bookType = bookTypeService.queryBookTypeById(bookTypeId);
        Integer integer = bookTypeService.deleteBookTypeById(bookTypeId);
        return RetType.retJson(RetJsonUtils.toJson("删除成功 类型："+bookType.getBtName()));
    }

    @YWebPath("/admin/adminOut")
    public RetType adminOut(HttpSession session){
        session.removeAttribute("admin");
        /*return RetType.retForward(DataInfo.PAGE_ROOT+"admin.jsp");*/
        return RetType.retRedirect("/");
    }


}
